#include <iostream>
#include <fstream>
#include <stdexcept>
#include <cmath> 
#include <vector>
#include "Function.hpp"
#include "Interpolation.hpp"

int main(){
    std::vector<std::vector<double>> matrix={
        {0,3,5,8,13},
        {0,225,383,623,993},
        {75,77,80,74,72}
    };
    HermiteInterpolation hermite(matrix);

    class F1_D : public Function {
        private:
        const HermiteInterpolation& hermite;

    public:
        F1_D(const HermiteInterpolation& hermite) : hermite(hermite) {}

        double operator()(double x) const override {
            // 直接使用 hermite 对象进行插值计算
            return hermite.interpolate(x);
        }
    };

    F1_D f(hermite);

    // 预测 t = 10 秒时的位置
    double t = 10.0;
    double predictedPosition = hermite.interpolate(t);
    std::cout << "Predicted position at t = " << t << " seconds: " << predictedPosition << " feet" << std::endl;

    // 预测 t = 10 秒时的速度
    double predictedvelocity = f.derivative(t);
    std::cout << "Predicted velocity at t = " << t << " seconds: " << predictedvelocity << " feet per second" << std::endl;
    
    // 检查速度是否超过 81 英尺/秒 
    std::ofstream file("data_D.csv");
        if (!file.is_open()) {
            std::cerr << "Failed to open file" << std::endl;
            return 1;
        }

        for (double x = -1; x <= 13; x += 0.01) {
            double y = f.derivative(x);
            file << x << "," << y << std::endl;
        }

        file.close();

        std::cout << "The car exceeds the speed limit" << std::endl;

    return 0;
}